local store = { data_curr = {}, data_prev = {} }
local max = 2000

function store.switch(self)
    self.data_prev = self.data_curr
    self.data_curr = {}
end

function store.listiter(self)
    local result = {}
    local keys = {}

    if next(self.data_prev) ~= nil then
        local k, v, vo, kr
        for k, v in pairs(self.data_curr) do
            vo = 0
            if self.data_prev[k] ~= nil then
                vo = self.data_prev[k]
            end
            if v >= vo then
                kr = v - vo
            else
                kr = v
            end
            result[k] = kr
            keys[#keys + 1] = k
        end

        table.sort(keys, function(k1, k2) return result[k2] < result[k1] end)
    end

    local i = 0
    return function()
        i = i + 1
        --ngx.log(ngx.ERR, result[keys[i]])
        if i <= max and keys[i] and result[keys[i]] > 0 then
            return keys[i], result[keys[i]]
        end
    end
end

function store.set(self, key, value)
    self.data_curr[key] = value
end

return store
